package com.ddwl.user.service;

import com.ddwl.common.constant.UserEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.constant.DistConstant;
import com.ddwl.schema.bo.user.SetDistrConfigBo;
import com.ddwl.schema.vo.dist.DistrConfigVo;
import com.ddwl.user.dao.UserLevelDao;
import com.ddwl.user.dao.model.UserLevel;
import com.ddwl.user.dao.DistrConfigDao;
import com.ddwl.user.dao.model.DistrConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DistrConfigServer {

    @Resource
    private DistrConfigDao distrConfigDao;
    @Resource
    private UserLevelDao userLevelDao;

    @Transactional(rollbackFor = Exception.class)
    public void setConfig(SetDistrConfigBo bo){
        DistrConfig disConfig=new DistrConfig();
        disConfig.setType(bo.getType());
        DistrConfig config =  distrConfigDao.select(disConfig).get(0);
        DistrConfig distrConfig = BeanConverUtils.convertBean(bo,DistrConfig.class);
        distrConfig.setId(config.getId());
        checkConfig(distrConfig);
        distrConfigDao.updateByPrimaryKeySelective(distrConfig);
        DistrConfig distrConfig1 = distrConfigDao.selectByPrimaryKey(config.getId());
        ParamException.isTrue(distrConfig1.getOneAgentRate()
                .add(distrConfig1.getTwoAgentRate())
                .add(distrConfig1.getThreeAgentRate())
                .add(distrConfig1.getRegionRate())
                .add(distrConfig1.getTopAgentRate()).compareTo(new BigDecimal(100))!=0,"佣金比例不正确");
    }

    private void checkConfig(DistrConfig distrConfig) {
        if (distrConfig.getTwoAgentRate()!=null){
            List<UserLevel> collect = userLevelDao.selectAll().stream().filter(o -> o.getDistRate().compareTo(distrConfig.getTwoAgentRate()) > 0).collect(Collectors.toList());
            ParamException.isTrue(collect!=null&&collect.size()!=0,"间接分润比例不能小于用户等级最高分润比例");
        }
    }

    /**
     * 获取代理分润值
     * @return
     */
    public BigDecimal getLevelRate(Integer level){
        UserEnum.ProfitLevelEnum profitLevel = UserEnum.ProfitLevelEnum.getLevel(level);
        if(profitLevel == null){
            return  null;
        }
        DistrConfigVo configVo = getDistrConfig(DistConstant.DistrConfigType.PRODUCT.getCode());
        switch (profitLevel){
            case PERSONAL : return configVo.getOneAgentRate();
            case STORE : return configVo.getTwoAgentRate();
            case ZONE : return configVo.getThreeAgentRate();
            default: return null;
        }
    }

    public DistrConfigVo getDistrConfig(Integer type){
        DistrConfig distrConfig=new DistrConfig();
        distrConfig.setType(type);
        return BeanConverUtils.convertBean(distrConfigDao.select(distrConfig).get(0),DistrConfigVo.class);
    }
}
